package com.stylefeng.guns.modular.cms.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.CmsArticle;
import com.stylefeng.guns.modular.cms.service.ICmsArticleService;

/**
 * 文章管理控制器
 *
 * @author fengshuonan
 * @Date 2018-06-24 23:57:05
 */
@Controller
@RequestMapping("/cmsArticle")
public class CmsArticleController extends BaseController {

    private String PREFIX = "/cms/cmsArticle/";

    @Autowired
    private ICmsArticleService cmsArticleService;

    /**
     * 跳转到文章管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "cmsArticle.html";
    }

    /**
     * 跳转到添加文章管理
     */
    @RequestMapping("/cmsArticle_add")
    public String cmsArticleAdd() {
        return PREFIX + "cmsArticle_add.html";
    }

    /**
     * 跳转到修改文章管理
     */
    @RequestMapping("/cmsArticle_update/{cmsArticleId}")
    public String cmsArticleUpdate(@PathVariable Integer cmsArticleId, Model model) {
        CmsArticle cmsArticle = cmsArticleService.selectById(cmsArticleId);
        model.addAttribute("item",cmsArticle);
        LogObjectHolder.me().set(cmsArticle);
        return PREFIX + "cmsArticle_edit.html";
    }

    /**
     * 获取文章管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return cmsArticleService.selectList(null);
    }

    /**
     * 新增文章管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CmsArticle cmsArticle) {
        cmsArticleService.insert(cmsArticle);
        return SUCCESS_TIP;
    }

    /**
     * 删除文章管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer cmsArticleId) {
        cmsArticleService.deleteById(cmsArticleId);
        return SUCCESS_TIP;
    }

    /**
     * 修改文章管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CmsArticle cmsArticle) {
        cmsArticleService.updateById(cmsArticle);
        return SUCCESS_TIP;
    }

    /**
     * 文章管理详情
     */
    @RequestMapping(value = "/detail/{cmsArticleId}")
    @ResponseBody
    public Object detail(@PathVariable("cmsArticleId") Integer cmsArticleId) {
        return cmsArticleService.selectById(cmsArticleId);
    }
}
